var organizationName;
var organizationId;
$(function () {
    init();
})

function init() {
    FastClick.attach(document.body);
    // putOptions($("#orgName"), "", orgInfo.list);
    var array=[];
    getOptions(array, "", orgInfo.list);
    // $("#orgName").select({
    //     title: "选择机构",
    //     items:array
    // });
    // $("#orgName").val(orgInfo.list[0].name);
    // $("#orgName").attr("data-values",orgInfo.list[0].id);
    loadWarehouse();
    // $("#orgName").on("change",function () {
    //     loadWorkSpace()
    // })
}
function putOptions(orgSelect, supername, list) {
    if (supername != "") supername = supername + "-";
    for (var i = 0; i < list.length; i++) {
        orgSelect.append("<option value=" + list[i].id + ">" + supername + list[i].name + "</option>")
        if (list[i].childrenOrg.length > 0) {
            putOptions(orgSelect, supername + list[i].name, list[i].childrenOrg);
        }
    }
}
function getOptions(array, supername, list) {
    if (supername != "") supername = supername + "-";
    for (var i = 0; i < list.length; i++) {
        array.push({
            title:supername + list[i].name,
            value: list[i].id
        })
        if (list[i].childrenOrg.length > 0) {
            getOptions(array, supername + list[i].name, list[i].childrenOrg);
        }
    }
}

function loadWarehouse() {
    var organizationId = $("#orgName").attr("data-values");
    warehouseSelect = [];
    $("#warehouseInfo").after("<input style=\"margin:10px;width:calc( 100% - 40px);\"" +
        "                                                                  class=\"weui-input\" id=\"warehouseInfo\" type=\"text\"" +
        "                                                                  value=\"\" readonly=\"\" data-values=\"\">");
    $("#warehouseInfo").eq(0).remove();
    equipmemtService.warehouseInfo({organization: organizationId}, function (data) {
        if (data.success) {
            var serverData = data.datas;
            if (serverData.length == 0)
                $("#warehouseInfo").val("暂无仓库信息");
            var array = []
            for (var i = 0; i < serverData.length; i++) {
                array.push({
                    title: serverData[i].name,
                    value: serverData[i].id
                })
            }
            warehouseSelect = array;
            $("#warehouseInfo").select({
                title: "选择仓库",
                items: array,
                onClose: function(d) {
                    beginSearch();
                }
            });
            $("#warehouseInfo").val(array[0].title);
            $("#warehouseInfo").attr("data-values",array[0].value);
            beginSearch();
        } else {
            $.toptip(data.errorMsg);
        }
    })
}

function beginSearch() {
    $("#blockC .warehouse").html("");
    equipmemtService.warehouseDetailInfo({
        warehouseId: getWarehouseId()
    }, function (data) {
        if (data.success) {
            data = data.datas;
            //排序
            data.sort(function(a,b){
                return (b.warnCount-b.count)-(a.warnCount-a.count)
            })
            $(data).each(function (i, item) {
                var ab = "a";
                if (item.warnCount - item.count >= 0) ab = "b";
                var percent = item.count / item.warnCount / 5 * 100;
                var ring;
                if (ab == "a") {
                    ring = $(
                        "<div class=\"button\">" +
                        "                    <div class=\"ring\">" +
                        "                        <div class=\"left\"><div class=\"leftafter\"></div></div>" +
                        "                        <div class=\"right\"> <div class=\"rightafter\"></div></div>" +
                        "                        <div class=\"progress\" ><span class=\"name\">" + item.name + "</span><br/><span class=\"tips\">库存充足</span></div>" +
                        "                    </div>" +
                        "                </div>");
                } else {
                    ring = $(
                        "<div class=\"button\">" +
                        "                    <div class=\"ring warn\">" +
                        "                        <div class=\"left\"><div class=\"leftafter\"></div></div>" +
                        "                        <div class=\"right\"> <div class=\"rightafter\"></div></div>" +
                        "                        <div class=\"progress\" ><span class=\"name\">" + item.name + "</span><br/><span class=\"tips\">库存不足</span></div>" +
                        "                    </div>" +
                        "                </div>");
                }
                $("#blockC .warehouse").append(ring);
                resizeRing(ring, percent);
                ring.on("click", function () {
                    equipmemtService.warehouseDetailList({
                        id: item.id,
                        warehouseId: getWarehouseId()
                    }, function (data) {
                        if (data.success) {
                            data = data.datas;
                            $(".weui-popup__container").remove();
                            var pop = $("<div class=\"weui-popup__container popup-bottom weui-popup__container--visible\" " +
                                "style=\"display: block; height: calc( 100% - 40px )\">" +
                                "      <div class=\"weui-popup__overlay\"></div>" +
                                "      <div class=\"weui-popup__modal\" style='height:100%'>" +
                                "        <div class=\"toolbar\">" +
                                "          <div class=\"toolbar-inner\">" +
                                "            <a href=\"javascript:;\" class=\"picker-button close-popup\">关闭</a>" +
                                "            <h1 class=\"title\">" + item.name + "</h1>" +
                                "          </div>" +
                                "        </div>" +
                                "        <div class=\"modal-content\">" +
                                "<div class='flex wareDetail'><div style='text-align: left;flex: 1;'><strong>当前库存：</strong></div><div style='text-align: left;flex: 1;'>" + item.count + "</div></div>" +
                                "<div class='flex wareDetail'><div style='text-align: left;flex: 1;'><strong>警告库存：</strong></div><div style='text-align: left;flex: 1;'>" + item.warnCount + "</div></div>" +
                                "<div class='flex wareDetail'><table border='0' cellpadding='0' cellspacing=\"0\"  width='100%'><thead><td>品牌</td><td>型号</td><td>库存数量</td>" +
                                // "<td>报警数量</td>" +
                                "</thead></table></div>" +
                                "        </div>" +
                                "      </div>" +
                                "    </div>");
                            $("body").append(pop);
                            $(data).each(function (i, item) {
                                pop.find("table").append("<tr><td>" + item.brandName + "</td><td>" + item.name + "</td><td>" + item.count + item.unit + "</td>" +
                                    // "<td>"+item.warnCount+"</td>" +
                                    "</tr>")
                            })

                        } else {
                            $.toptip(data.errorMsg);
                        }
                    })
                })
            })
        } else {
            $.toptip(data.errorMsg);
        }
    })
}

var warehouseSelect = [];

function getWarehouseId() {
    if ($("#warehouseInfo").attr("data-values") == "" || $("#warehouseInfo").attr("data-values") == "0") {
        if (warehouseSelect.length > 0) {
            return warehouseSelect[0].value;
        } else return null;
    } else {
        return $("#warehouseInfo").attr("data-values");
    }
}
function resizeRing(ring, percent) {
    var width = $(".ring").parent().width();
    $(".ring").css("font-size", width / 8.2 + "px");
    console.info(percent);
    if (percent >= 100) {
        ring.find(".leftafter").css('transform', "rotateZ(0deg)");
        ring.find(".rightafter").css('transform', "rotateZ(0deg)");
    } else if (percent > 50) {
        ring.find(".leftafter").css('transform', "rotateZ(0deg)");
        ring.find(".rightafter").css('transform', "rotateZ(" + 180 * (100 - percent) / 100 + "deg)");
    } else {
        ring.find(".leftafter").css('transform', "rotateZ(" + 180 * (50 - percent) / 50 + "deg)");
        ring.find(".rightafter").css('transform', "rotateZ(180deg)");
    }
}
